<?php

class  Lib_SMS_Base_Tool
{



    private static $pre_fix = 'lib_sms_check_scene_';
    /**
     * 验证手机号码
     * @param unknown $mobile
     * @return boolean|unknown
     */
    public static function checkmobile($mobile){
        $rule = '/^1[3|4|5|6|7|8|9][\d]{9}/';
        preg_match($rule, $mobile, $regex);
        if(!$regex[0] || strlen($mobile) > 11){
            return false;
        }
        return $mobile;
    }
    /**
     * 过滤SPAM垃圾短信
     * @param $scene
     * @param $mobile
     * @return bool|int
     */
    public static function check_spam($mobile)
    {
        //验证发送短信间隔时间 60秒
        $key = self::$pre_fix . '_mobile_' . $mobile;
        $value = Func_Cachefunc::getInstance()->get($key);
        if ($value) {
            return 5502;
        }

        //验证发送次数，每天限制10条
        $res = Lib_Redisfunc::getInstance()->checkceiling('sms', $mobile, 10);
        if ($res === false) {
            return 5501;
        }
        Func_Cachefunc::getInstance()->set($key, time(), 60);

        return true;

    }

    /**
     * 验证QPS
     * @return bool|int
     */
    public static function check_QPS($log)
    {
        if(PHP_SAPI === 'cli'){
            return true;
        }
        //验证发送短信间隔时间 1秒
        $key = self::$pre_fix . "QPS";
        //获取当前吞吐量
        $num = Lib_Redis_String::get($key);
        if($num > 20){
            return 5505;
        }
        $obj = new Lib_Redis_String;
        if(!$num){
            $obj->setex($key, 1, 1);

        }else{
            $obj->incr($key);
        }
        unset($obj);
        return true;

    }

    /**
     * 手机黑名单校验
     * @param $mobile
     * @return bool|int
     */
    public static function check_black($mobile)
    {
        $black_mobile = Lib_SMS_Config::$black_mobile;
        if (in_array($mobile, $black_mobile)) {
            return 5503;
        }
        return true;
    }


    /**
     * 格式化短信内容
     * @param $mould_txt
     * @param array $argv
     * @return mixed
     * @throws Exception
     */
    public static function formatText($mould_txt, $argv = array())
    {

        preg_match_all("/\{.*\}/U", $mould_txt, $res_arr);

        //正则发生错误
        if ($res_arr === false) {
            throw new Exception('短信内容异常',4005);
        }

        //短信参数必须与配置数量相等
        if (count($res_arr[0]) != count($argv)) {
            throw new Exception('短信变量与模板不匹配',4006);
        }

        foreach ($argv as $v) {
            if(empty($v)){
                throw new Exception('短信变量内容为空',4006);
            }
            $mould_txt = preg_replace("/\{.*?\}/", $v, $mould_txt, 1);
        }

        return $mould_txt;
    }

    /**
     * 防止同一ip刷短信
     * @param $app
     * @param $scene
     * @return bool
     */
    public static function check_ip(){
        $ip = Lib_Function::getInstance()->getip();
        if(!$ip) return true;
        //验证发送短信间隔时间 60秒
        $key = self::$pre_fix. '_ip_' . $ip;
        $value = Func_Cachefunc::getInstance()->get($key);
        if ($value) {
            return false;

        }
        //验证发送次数，每天限制10条
        $res = Lib_Redisfunc::getInstance()->checkceiling('sms_', $ip, 100);
        if ($res === false) {
            return false;
        }
        Func_Cachefunc::getInstance()->set($key, time(), 2);
        return true;
    }

    public static function buildArgv($data){
        $argv = [];
        foreach ($data as $key => $val){
            switch ($key){
                case 'variableOne':
                    $argv[1] = $val;
                    break;
                case 'variableTwo':
                    $argv[2] = $val;
                    break;
                case 'variableThree':
                    $argv[3] = $val;
                    break;
                case 'variableFour':
                    $argv[4] = $val;
                    break;
                case 'variableFive':
                    $argv[5] = $val;
                    break;
                case 'variableSix':
                    $argv[6] = $val;
                    break;
                case 'variableSeven':
                    $argv[7] = $val;
                    break;
            }

        }
        ksort($argv);
        return $argv;
    }

}